package com.mikgonz.farkle;

import java.util.ArrayList;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class GameScreen extends Activity {

	private Resources r;
	private TextView scoreBox;
	private TextView messageBox;
	private Button scoreButton;
	private Button rollButton;
	private GameController controller;
	private ArrayList<MyImageView> imageViews = new ArrayList<MyImageView>();
	private int[] letters = new int[6];
	private boolean toFarkle = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.game_screen);
		r = getResources();
		initializeArrays();
		scoreBox = (TextView) findViewById(R.id.score_box);
		scoreButton = (Button) findViewById(R.id.score_button);
		rollButton = (Button) findViewById(R.id.roll_button);
		messageBox = (TextView) findViewById(R.id.message_text);

		// Restore GameController

		if (getLastNonConfigurationInstance() != null) {
			controller = (GameController) getLastNonConfigurationInstance();
			controller.newUI(this);
			updateImages(false, false);
		} else {
			controller = new GameController(3, this);
		}

		// Restore UI elements

		if (savedInstanceState != null
				&& savedInstanceState.getBoolean("hasState")) {

			scoreBox.setText(savedInstanceState.getCharSequence("scoreText"));
			messageBox.setText(savedInstanceState
					.getCharSequence("messageText"));

			scoreButton.setEnabled(savedInstanceState.getBoolean("scoreState"));
			rollButton.setEnabled(savedInstanceState.getBoolean("rollState"));
		}
	}

	private void initializeArrays() {
		imageViews.add((MyImageView) findViewById(R.id.img_1));
		imageViews.add((MyImageView) findViewById(R.id.img_2));
		imageViews.add((MyImageView) findViewById(R.id.img_3));
		imageViews.add((MyImageView) findViewById(R.id.img_4));
		imageViews.add((MyImageView) findViewById(R.id.img_5));
		imageViews.add((MyImageView) findViewById(R.id.img_6));

		letters[0] = R.drawable.f;
		letters[1] = R.drawable.a;
		letters[2] = R.drawable.r;
		letters[3] = R.drawable.k;
		letters[4] = R.drawable.l;
		letters[5] = R.drawable.e;

	}

	public void onRoll(View v) {

		controller.onRoll();
	}

	public void onScore(View v) {
		controller.onScore();
	}

	public void onClickDice(View v) {

		controller.onClickDice(DieManager.identifyView(v));
	}

	public void rollButtonState(boolean state) {
		rollButton.setEnabled(state);
	}

	public void scoreButtonState(boolean state) {
		scoreButton.setEnabled(state);
	}

	public void displayMessage(String string) {

		messageBox.setText(string);
	}

	public void updateScoreBox(String string) {

		scoreBox.setText(string);
	}

	public void updateImages(boolean toAnimate, boolean isFarkle) {

		Animation a = AnimationUtils.loadAnimation(this, R.anim.dice_animation);
		a.reset();

		MyImageView i = null;
		for (int j = 0; j < imageViews.size(); j++) {

			i = imageViews.get(j);

			if (isFarkle) {
				i.isFarkle(this);
			}

			i.setImageDrawable(r.getDrawable(controller.getImage(j)));
			if (toAnimate && controller.shouldAnimate(j)) {
				i.clearAnimation();
				i.startAnimation(a);
			}

		}

		if (isFarkle) {
			controller.clearTable();
			toFarkle = true;
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBoolean("hasState", true);
		outState.putBoolean("scoreState", scoreButton.isEnabled());
		outState.putBoolean("rollState", rollButton.isEnabled());
		outState.putCharSequence("scoreText", scoreBox.getText());
		outState.putCharSequence("messageText", messageBox.getText());

		super.onSaveInstanceState(outState);
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		return controller;
	}

	public void showFarkle() {
		if (toFarkle) {

			MyImageView i = null;
			for (int j = 0; j < imageViews.size(); j++) {

				Animation a = AnimationUtils.loadAnimation(this,
						R.anim.dice_animation);
				a.setStartOffset(j * 200);
				i = imageViews.get(j);
				i.setImageDrawable(r.getDrawable(letters[j]));
				i.clearAnimation();
				i.startAnimation(a);

			}
			controller.clearTable();
			toFarkle = false;
		}
	}
}
